package com.asit.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/kafka")
@Slf4j
public class KafKaCtrol {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
    @RequestMapping("/send/{msg}/{len}")
    public String haha(@PathVariable String msg,@PathVariable Integer len){
        JSONObject json = new JSONObject();
        json.put("msg",msg);
        for(int i=0;i<len;i++){
            json.put("age", i+"="+len);
            this.kafkaTemplate.send("mtytopic", json.toJSONString());
        }
        //log.info("{}",kafkaTemplate);
        return "发送成功["+msg+"]";
    }
}
